package com.yxp.offer;

import com.yxp.util.ListNode;

/**
 * @author : yang.xp
 * @date : 2021/8/24 9:26
 * @description :
 */
public class Problem25 {

    public static void main(String[] args) {
        ListNode listNode1 = ListNode.initListNode(new int[]{1, 2, 3});
        ListNode listNode2 = ListNode.initListNode(new int[]{1, 2, 4});
        Problem25 problem25 = new Problem25();
        System.out.println(problem25.mergeTwoLists(listNode1, listNode2));
    }

    // 都往l2上添加
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1.val <= l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}
